< previous page page_251 next page >

Page 251
At this point, Visual Basic adds a Sprite section to the class's code. If you click on the left ComboBox in the class's code window, you will see the Sprite section. Select that section, and you will find that Visual Basic has defined stub routines named Sprite_MoveSprite and Sprite_DrawSprite. Enter the code needed by the particular sprite class in those routines.
The following code shows a complete BallSprite class. The private variables and the InitializeBall method are specific to this class and are not defined by the generic Sprite class. Other sprite classes will have their own variables and initialization methods.
e10a6a0a59f035e870d90cca05b85fce.gif
' Bouncing ball sprite.
Option Explicit 

Implements Sprite

Private Radius As Integer
Private Cx As Integer       ' Position of center.
Private Cy As Integer
Private Vx As Integer       ' Velocity.
Private Vy As Integer
Private Color As Long

' Initialize the ball.
Public Sub InitializeBall(ByVal new_radius As Integer, _
    ByVal new_cx As Integer, ByVal new_cy As Integer, _
    ByVal new_vx As Integer, ByVal new_vy As Integer, _
    ByVal new_color As Long)

    Radius = new_radius
    Cx = new_cx
    Cy = new_cy
    Vx = new_vx
    Vy = new_vy
    Color = new_color
End Sub

' Add the velocity components to the sprite's
' position components.
Public Sub Sprite_MoveSprite( _
    ByVal xmin As Integer, ByVal xmax As Integer, _
    ByVal ymin As Integer, ByVal ymax As Integer)

    Cx = Cx + Vx 
    Cy = Cy + Vy 

    ' Keep the object within the drawing area. 
    If (Cx < xmin) Then 
        Cx = 2 * xmin - Cx 
        Vx = -Vx 
    ElseIf (Cx > xmax) Then 
        Cx = 2 * xmax - Cx

 
< previous page page_251 next page >